#Plots for main analysis

library(ggplot2)
library(readxl)
library("viridis")  
library(RColorBrewer)
setwd("/Users/angelachesler/Documents/Documents - Angela’s MacBook Pro (3)/Working Papers/Triggers of Mass Atrocities/ISQ_Triggers of Mass Killing")

pcr_plot_data<-read_xlsx("pcr_plot_data.xlsx")
pcr_plot_data$pcr[pcr_plot_data$pcr==0]<-0.005

pcr_plot_data$control<-factor(pcr_plot_data$control, levels=c("All", "Democracy", "Hybrid", "Autocracy"))
pcr_plot_data_conflict_escalation<-pcr_plot_data[which(pcr_plot_data$trigger=="Gov. battle loss" | pcr_plot_data$trigger=="Rebel civilian killing" | pcr_plot_data$trigger=="Conflict spillover"), ] 

pcr_plot_data$trigger<-factor(pcr_plot_data$trigger, 
                              levels=c("Gov. battle loss", "Rebel civilian killing", "Conflict spillover", "Nonviolent protest", "Violent protest", "Attempted coup", "Successful coup"))

png(filename = "pcr_plot_times.png", width = 6, height = 6, units = "in", res=2500)
ggplot(pcr_plot_data[which(pcr_plot_data$control=="All"), ], aes(x=trigger, y=pcr, fill=time)) +
  geom_col(position= position_dodge(0.9), width=.8) + xlab("") + ylab("Precursor coincidence rate") +
  theme_bw() + scale_fill_viridis(discrete = TRUE, begin=0, end=.7, alpha=.9) + 
  labs(fill="") + scale_y_continuous(limits=c(0, .3), breaks=seq(0, .3, by=.05)) +
  theme(legend.position="bottom", legend.text=element_text(size=16), axis.text.x = element_text(angle = 60, vjust = 1, hjust=1, size=16), axis.text.y = element_text(size=16), axis.title.y = element_text(size=16))
dev.off()


png(filename = "pcr_plot_regimes.png", width = 6, height = 6, units = "in", res=2500)
ggplot(pcr_plot_data[which(pcr_plot_data$time=="1-4 months"), ], aes(x=trigger, y=pcr, fill=control)) +
  geom_col(position= position_dodge(0.9), width=.8) + xlab("") + ylab("Precursor coincidence rate") +
  theme_bw() + scale_fill_viridis(discrete = TRUE, begin=.2, end=1, alpha=.9) + 
  scale_y_continuous(limits=c(0,.3), breaks=seq(0, .3, by=.05)) + 
  labs(fill="") +  #   labs(fill="") + 
  theme(legend.position="bottom", legend.text=element_text(size=16), axis.text.x = element_text(angle = 60, vjust = 1, hjust=1, size=16), axis.text.y = element_text(size=16), axis.title.y = element_text(size=16))

dev.off()






